<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div>
  <mat-card appearance="outlined" class="settings-card">
    <mat-card-header>
      <mat-card-title>
        <span class="mat-headline-5" translate>admin.outgoing-mail-settings</span>
      </mat-card-title>
      <span class="flex flex-1"></span>
      <div tb-help="outgoingMailSettings"></div>
    </mat-card-header>
    <mat-card-content style="padding-top: 16px;">
      <form [formGroup]="mailSettings" (ngSubmit)="save()">
        <fieldset [disabled]="isLoading$ | async">
          <mat-form-field class="mat-block">
            <mat-label translate>admin.mail-from</mat-label>
            <input matInput formControlName="mailFrom" required/>
            <mat-icon matSuffix
                      matTooltipClass="tb-error-tooltip"
                      matTooltip="{{ 'admin.mail-from-required' | translate }}"
                      [class.!hidden]="!(mailSettings.get('mailFrom').hasError('required') && mailSettings.get('mailFrom').touched)"
                      class="tb-error">
              warning
            </mat-icon>
          </mat-form-field>
          <mat-expansion-panel class="configuration-panel mat-elevation-z0" [expanded]="true">
            <mat-expansion-panel-header>
              <mat-panel-title class="flex items-center" translate>
                admin.connection-settings
              </mat-panel-title>
            </mat-expansion-panel-header>
            <ng-template matExpansionPanelContent>
              <mat-form-field class="mat-block">
                <mat-label translate>admin.smtp-protocol</mat-label>
                <mat-select formControlName="smtpProtocol">
                  @for (protocol of smtpProtocols; track protocol) {
                    <mat-option [value]="protocol">
                      {{protocol.toUpperCase()}}
                    </mat-option>
                  }
                </mat-select>
              </mat-form-field>
              <div class="gt-sm:flex gt-sm:flex-row gt-sm:gap-2.5">
                <mat-form-field class="flex flex-1 gt-sm:basis-[60%]">
                  <mat-label translate>admin.smtp-host</mat-label>
                  <input matInput formControlName="smtpHost" placeholder="localhost" required/>
                  <mat-icon matSuffix
                            matTooltipClass="tb-error-tooltip"
                            matTooltip="{{ 'admin.smtp-host-required' | translate }}"
                            [class.!hidden]="!(mailSettings.get('smtpHost').hasError('required') && mailSettings.get('smtpHost').touched)"
                            class="tb-error">
                    warning
                  </mat-icon>
                </mat-form-field>
                <mat-form-field class="flex flex-1 gt-sm:basis-[40%]">
                  <mat-label translate>admin.smtp-port</mat-label>
                  <input matInput #smtpPortInput formControlName="smtpPort" placeholder="25" maxlength="5" required/>
                  <mat-hint align="end">{{smtpPortInput.value?.length || 0}}/5</mat-hint>
                  <mat-icon matSuffix
                            matTooltipClass="tb-error-tooltip"
                            matTooltip="{{ 'admin.smtp-port-required' | translate }}"
                            [class.!hidden]="!(mailSettings.get('smtpPort').hasError('required') && mailSettings.get('smtpPort').touched)"
                            class="tb-error">
                    warning
                  </mat-icon>
                  <mat-icon matSuffix
                            matTooltipClass="tb-error-tooltip"
                            matTooltip="{{ 'admin.smtp-port-invalid' | translate }}"
                            [class.!hidden]="!((mailSettings.get('smtpPort').hasError('pattern') || mailSettings.get('smtpPort').hasError('maxlength')) && mailSettings.get('smtpPort').touched)"
                            class="tb-error">
                    warning
                  </mat-icon>
                </mat-form-field>
              </div>
              <mat-form-field class="mat-block">
                <mat-label translate>admin.timeout-msec</mat-label>
                <input matInput #timeoutInput formControlName="timeout" placeholder="10000" maxlength="6" required/>
                <mat-hint align="end">{{timeoutInput.value?.length || 0}}/6</mat-hint>
                <mat-icon matSuffix
                          matTooltipClass="tb-error-tooltip"
                          matTooltip="{{ 'admin.timeout-required' | translate }}"
                          [class.!hidden]="!(mailSettings.get('timeout').hasError('required') && mailSettings.get('timeout').touched)"
                          class="tb-error">
                  warning
                </mat-icon>

                <mat-icon matSuffix
                          matTooltipClass="tb-error-tooltip"
                          matTooltip="{{ 'admin.timeout-invalid' | translate }}"
                          [class.!hidden]="!((mailSettings.get('timeout').hasError('pattern') || mailSettings.get('timeout').hasError('maxlength')) && mailSettings.get('timeout').touched)"
                          class="tb-error">
                  warning
                </mat-icon>
              </mat-form-field>
              <mat-slide-toggle class="flex flex-1" formControlName="enableTls" style="display: block; padding-bottom: 22px;">
                {{ 'admin.enable-tls' | translate }}
              </mat-slide-toggle>
              @if (mailSettings.get('enableTls').value) {
                <mat-form-field class="flex flex-1">
                  <mat-label translate>admin.tls-version</mat-label>
                  <mat-select formControlName="tlsVersion">
                    @for (tlsVersion of tlsVersions; track tlsVersion) {
                      <mat-option [value]="tlsVersion">
                        {{ tlsVersion }}
                      </mat-option>
                    }
                  </mat-select>
                </mat-form-field>
              }
              <mat-slide-toggle formControlName="enableProxy" style="display: block; padding-bottom: 22px;">
                {{ 'admin.enable-proxy' | translate }}
              </mat-slide-toggle>
              @if (mailSettings.get('enableProxy').value) {
                <div>
                  <div class="gt-sm:flex gt-sm:flex-row gt-sm:gap-2">
                    <mat-form-field class="flex flex-1 gt-sm:basis-[60%]">
                      <mat-label translate>admin.proxy-host</mat-label>
                      <input matInput required formControlName="proxyHost">
                      <mat-icon matSuffix
                                matTooltipClass="tb-error-tooltip"
                                matTooltip="{{ 'admin.proxy-host-required' | translate }}"
                                [class.!hidden]="!(mailSettings.get('proxyHost').hasError('required') && mailSettings.get('proxyHost').touched)"
                                class="tb-error">
                        warning
                      </mat-icon>
                    </mat-form-field>
                    <mat-form-field class="flex flex-1 gt-sm:basis-[40%]">
                      <mat-label translate>admin.proxy-port</mat-label>
                      <input matInput required formControlName="proxyPort" type="number" step="1" min="1" max="65535">
                      <mat-icon matSuffix
                                matTooltipClass="tb-error-tooltip"
                                matTooltip="{{ 'admin.proxy-port-required' | translate }}"
                                [class.!hidden]="!(mailSettings.get('proxyPort').hasError('required') && mailSettings.get('proxyPort').touched)"
                                class="tb-error">
                        warning
                      </mat-icon>
                      <mat-icon matSuffix
                                matTooltipClass="tb-error-tooltip"
                                matTooltip="{{ 'admin.proxy-port-range' | translate }}"
                                [class.!hidden]="!((mailSettings.get('proxyPort').hasError('pattern') || mailSettings.get('proxyPort').hasError('min') || mailSettings.get('proxyPort').hasError('max')) && mailSettings.get('proxyPort').touched)"
                                class="tb-error">
                        warning
                      </mat-icon>
                    </mat-form-field>
                  </div>
                  <div class="flex flex-row xs:flex-col gt-xs:gap-2">
                    <mat-form-field class="flex flex-1">
                      <mat-label translate>admin.proxy-user</mat-label>
                      <input matInput formControlName="proxyUser">
                    </mat-form-field>
                    <mat-form-field class="flex flex-1">
                      <mat-label translate>admin.proxy-password</mat-label>
                      <input matInput type="password" formControlName="proxyPassword" autocomplete="new-proxy-password">
                      <tb-toggle-password matSuffix></tb-toggle-password>
                    </mat-form-field>
                  </div>
                </div>
              }
            </ng-template>
          </mat-expansion-panel>
          <fieldset class="fields-group flex flex-col">
            <legend class="group-title" translate>admin.authentication</legend>
            <mat-form-field class="mat-block">
              <mat-label translate>common.username</mat-label>
              <input matInput formControlName="username" placeholder="{{ 'common.enter-username' | translate }}"
                autocomplete="new-username"/>
            </mat-form-field>
            <section>
              @if (showChangePassword) {
                <mat-checkbox formControlName="changePassword" style="padding-bottom: 16px;">
                  {{ 'admin.change-password' | translate }}
                </mat-checkbox>
              }
              @if (mailSettings.get('changePassword').value || !showChangePassword) {
                <mat-form-field class="mat-block">
                  <mat-label translate>common.password</mat-label>
                  <input matInput formControlName="password" type="password"
                    placeholder="{{ 'common.enter-password' | translate }}" autocomplete="new-password"/>
                  <tb-toggle-password matSuffix></tb-toggle-password>
                </mat-form-field>
              }
            </section>
          </fieldset>
          <div class="flex flex-row items-center justify-end gap-4 xs:flex-col xs:justify-end">
            <button mat-raised-button type="button"
              [disabled]="(isLoading$ | async) || mailSettings.invalid" (click)="sendTestMail()">
              {{'admin.send-test-mail' | translate}}
            </button>
            <button mat-raised-button color="primary"
              [disabled]="(isLoading$ | async) || mailSettings.invalid || !mailSettings.dirty"
              type="submit">{{'action.save' | translate}}
            </button>
          </div>
        </fieldset>
      </form>
    </mat-card-content>
  </mat-card>
</div>
